﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.SystemUI;
using PlanningGIS.Util.Regular;
using System.Xml.Serialization;
using PlanningGIS.Util.Data;
using PlanningGIS.Oracle;
//using PlanningGIS.Access;

namespace PlanningGIS.Framework.Config
{
    /// <summary>
    /// 数据库连接配置信息
    /// </summary>
    public static class DbConnectHelper
    {
        
        /// <summary>
        /// 根据名称获取数据库连接实例
        /// </summary>
        /// <param name="intancename">实例名称</param>
        /// <returns></returns>
        public static DbConnectionInfo GetDbConnection(string intancename)
        {
            if (AppConfig.SystemConfig == null || AppConfig.SystemConfig.DbConnections == null ||
                AppConfig.SystemConfig.DbConnections.Count == 0)
                return null;
            DbConnectionInfo info = AppConfig.SystemConfig.DbConnections.FirstOrDefault(t => t.Name.Equals(intancename, StringComparison.CurrentCultureIgnoreCase));
            if (info != null)
            {
                if (AppConfig.SystemConfig.基本信息.HasEnCrypt)
                {
                    string passWord = PlanningGIS.Util.Security.SecurityHelper.DeCryptDES2(info.Password, "zjGIS2008");
                    if (!String.IsNullOrWhiteSpace(passWord))
                        info.Password = passWord;
                }
            }
            return info;
        }

        /// <summary>
        /// 根据配置的实例名称创建数据库操作对象
        /// </summary>
        /// <param name="intancename"></param>
        /// <returns></returns>
        public static IDbContext CreateDbContextByIntance(string intancename)
        {
            if (AppConfig.SystemConfig == null || AppConfig.SystemConfig.DbConnections == null || AppConfig.SystemConfig.DbConnections.Count == 0)
                return null;
            DbConnectionInfo db = GetDbConnection(intancename);
            if (db == null) return null;
            return CreateDbContext(db);
        }

        /// <summary>
        /// 创建数据库连接操作对象
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static IDbContext CreateDbContext(DbConnectionInfo entry)
        {
            if (entry == null || string.IsNullOrWhiteSpace(entry.Server)) return null;
            if ("Access".Equals(entry.DBType, StringComparison.CurrentCultureIgnoreCase))
                return null;
            // return new AccessContext() { ConnectionString = entry.GetConnectString() } as IDbContext;
            else if ("Sql".Equals(entry.DBType, StringComparison.CurrentCultureIgnoreCase))
                return null;
            else
                return new OracleContext() { ConnectionString = entry.GetConnectString() } as IDbContext;
        }

        /// <summary>
        /// 根据数据库连接信息获取数据库连接操作对象
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static IDbContext GetDbContext(this DbConnectionInfo entry)
        {
            return CreateDbContext(entry);
        }
    
    }
}
